package com.edmond.controller;

import com.edmond.entity.User;
import com.edmond.entity.UserVO;
import com.edmond.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
@EnableAutoConfiguration
public class UserHandler {
    @Autowired
    private UserFeign userFeign;

    //  查询所有用户数据
    @GetMapping("/findAll")
    @ResponseBody
    public UserVO findAll(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        UserVO userVO = new UserVO();
        userVO.setCode(0);
        userVO.setMsg("success");
        userVO.setCount(userFeign.count());
        userVO.setData(userFeign.findAll((page - 1) * limit, limit));
        return userVO;
    }

    //  新增用户信息
    //  前端向后端传送参数，通过的是表单 ，不是json数据 不需要加上 @RequestBody 注解
    @PostMapping("/save")
    public String save(User user) {
        user.setRegisterdate(new Date());
        userFeign.save(user);
        return "redirect:/user/redirect/user_manage";
    }

    //  测试layui 是否引入的页面跳转功能
    @GetMapping("/redirect/{location}")
    public String redirect(@PathVariable("location") String location) {
        return location;
    }

    @GetMapping("/deleteById/{id}")
    public String deleteById(@PathVariable("id") long id){
        userFeign.deleteById(id);
        return "redirect:/user/redirect/user_manage";
    }
}
